import axios from 'axios'
// import { getStorage } from '@/utils/storage.js'
import store from '@/store'// 通过vuex拿到数据
import JSONBig from 'json-bigint'

const request = axios.create({
  baseURL: 'http://toutiao-app.itheima.net',
  timeout: 3000,
  transformResponse: [function (data) {
    // 对 data 进行任意转换处理
    try {
      return JSONBig.parse(data)
    } catch (err) {
      return data
    }
  }]
})

// 拦截所有利用request发出的请求
request.interceptors.request.use((config) => {
  // console.log('请求拦截器')
  if (store.state.user) {
    config.headers.Authorization = 'Bearer ' + store.state.user.token
  }
  return config
})

// 拦截所有响应的结果
request.interceptors.response.use(res => {
  return res.data.data
})

export default request
